package com.km.config;

import com.km.entity.Tags;
import com.km.repository.TagsRepository;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import java.util.List;

/**
 * <p></p>
 * Created by zhezhiyong@163.com on 2017/10/12.
 */
@Component
public class InitSystem implements ServletContextListener, ApplicationContextAware {

    private static ApplicationContext applicationContext;


    private void loadData(ServletContext application) {
        TagsRepository tagsRepository = (TagsRepository) applicationContext.getBean("tagsRepository");
        List<Tags> tagsList = tagsRepository.findAll();
        application.setAttribute("tagsList", tagsList); // 标签云
    }


    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.applicationContext = applicationContext;
    }

    @Override
    public void contextInitialized(ServletContextEvent servletContextEvent) {
        loadData(servletContextEvent.getServletContext());
    }

    @Override
    public void contextDestroyed(ServletContextEvent servletContextEvent) {

    }

}
